package com.zz.controller;

import com.zz.util.EditPdfUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/edit")
public class EditPdfController {

    @Value("${mergePdfpath}")
    private String mergePdfpath;

    @Value("${mergePdfFolder}")
    private String mergePdfFolder;

    @RequestMapping("/merge")
    public String mergepdf(){
        return "mergetest";
    }

    @PostMapping("/domerge")
    @ResponseBody
//    public String multiImport(@RequestParam("mergefile[]") MultipartFile[] mergefile, HttpServletRequest request) throws IOException {
    public String multiImport(@RequestParam("mergefile[]") MultipartFile[] mergefile,
                              @RequestParam("mergename") String mergename) throws IOException {
//        Map<String, Object> result = new HashMap<String, Object>();
        System.out.println(mergefile.length);

        List<File> fileList=new ArrayList<File>();
//        MultipartFile multipartFile= multiReq.getFile("files");
//        System.out.println(multipartFile);
        File folder=new File(mergePdfFolder);
        if(!folder.exists()){
            folder.mkdirs();
            System.out.println(folder);
        }
        for (MultipartFile multipartFile:mergefile) {
            String filename=multipartFile.getOriginalFilename();  //传入的单个文件名字
            System.out.println("传入了文件"+filename);
            String filepath=mergePdfpath+filename;
            File file=new File(mergePdfpath+filename);

            multipartFile.transferTo(file);   //将文件传入
            fileList.add(file);
        }
//        String mergename=request.getParameter("mergename");

//        通过json传来的mergename是"name=xxx.xx" 需要把文件名字xx截取出来
        String mergenamestr=mergename.substring(mergename.lastIndexOf('=')+1,mergename.lastIndexOf('"'));
        System.out.println(mergenamestr);

//        String mergep=mergePdfpath+"first.pdf";
//        String mergep=mergePdfpath+mergename;
        String mergep=mergePdfpath+mergenamestr;

        EditPdfUtil editPdfUtil=new EditPdfUtil();
        editPdfUtil.MergePdf(fileList,mergep);
//
        return "files upload success";
//        return result;

    }

    @RequestMapping("/dodel")
    @ResponseBody
    public int DelPdfPages(@RequestParam("delfile") MultipartFile delFile,
        @RequestParam("fromp") int fromp,@RequestParam("endp") int endp) throws IOException {

        String oldfilename=delFile.getOriginalFilename();
        System.out.println("要删除页数的文件是"+oldfilename);

//        Resource getResource=delFile.getResource();
//        String getName=delFile.getName();
//        InputStream getInputStream=delFile.getInputStream();
//        System.out.println("getResource:  "+getResource);
//        System.out.println("getName:  "+getName);
//        System.out.println("getInputStream:  "+getInputStream);

//     剪切文件名
        String filename=oldfilename.substring(0,oldfilename.lastIndexOf('.'));
        int length=oldfilename.length();
        String suffix=oldfilename.substring(oldfilename.lastIndexOf('.'),length);
        String newfilename=filename+"_new"+suffix;

        String oldpdfpath=mergePdfpath+oldfilename;
        String newpdfpath=mergePdfpath+newfilename;

        System.out.println("fromp,endp:  "+fromp+"、"+endp);
        EditPdfUtil editPdfUtil=new EditPdfUtil();
        int result=editPdfUtil.DelPdf(oldpdfpath,newpdfpath,fromp,endp);

        return result;
    }

}
